package cc.imos.communal.entity;

import cc.imos.communal.err.ErrException;
import cc.imos.communal.err.ErrCode;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * <pre>
 * 通用的接口传输对象
 * body 已经初始化可以直接 put 值
 * @author asher
 * Created by asher on 2017/3/11.
 * </pre>
 */
@Data
@AllArgsConstructor
@ApiModel(value = "响应对象")
@JsonPropertyOrder(value = {"code", "message", "body"}, alphabetic = true)
public class Rest<T> {
    @ApiModelProperty(value = "响应代码", required = true)
    private String code;

    @ApiModelProperty(value = "响应消息", required = true)
    private String message;

    @ApiModelProperty(value = "响应内容", required = true)
    private T body;

    /**
     * 默认成功
     */
    public Rest() {
        this.code = ErrCode.ok.name();
        this.message = ErrCode.ok.getMessage();
    }

    public Rest(ErrCode errCode) {
        this.code = errCode.name();
        this.message = errCode.getMessage();
    }

    public Rest(ErrCode errCode, String message) {
        this.code = errCode.name();
        this.message = message;
    }

    public Rest(ErrException err) {
        this.code = err.getCode();
        this.message = err.getMessage();
    }

    public Rest(ErrException err, String message) {
        this.code = err.getCode();
        this.message = message;
    }

    public Rest set(ErrCode errCode) {
        this.code = errCode.name();
        this.message = errCode.getMessage();
        return this;
    }

    public Rest set(ErrCode errCode, String message) {
        this.code = errCode.name();
        this.message = message;
        return this;
    }

    public Rest set(ErrException err) {
        this.code = err.getCode();
        this.message = err.getMessage();
        return this;
    }

    public Rest set(ErrException err, String message) {
        this.code = err.getCode();
        this.message = message;
        return this;
    }
}
